



import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import watij.elements.*;
import watij.runtime.ie.*;

/**
 * Servlet implementation class FetchUIElements
 */
public class FetchUIElements extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FetchUIElements() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter pw = response.getWriter();									
		String urlTest = request.getParameter("urlTest");				//fetching url to be tested and storing it in a string
		System.out.println(urlTest);
		try
		{
			IE ie = new IE();
			ie.start(urlTest);
			TextFields textfield = ie.textFields();						//referring all textfields on the page
			Radios radiobutton = ie.radios();							//referring all radiobuttons in the page
			Checkboxes checkbox = ie.checkboxes();						//referring all checkboxes in the page
			SelectLists list = ie.selectLists();						//referring all combo boxes in the page
			Hiddens hidden = ie.hiddens();								//referring all hidden elements in the page
			int finalCount = 0;											//to store total no of elements
			int tempCount = 0;											//temporary variable to store the position of current element
			int id = 0;													//for providing id to every object of data array
																		//value of id at any point is finalCount+tempCount
			String jsonResponse = "";									//final response string containing all the details
			String string1 = "[";
			int i;
			
			boolean flag = false;										//flag is used for checking whether trailing "," is added to the string or not
			for(i=0;i<textfield.length();i++)
			{
				if(i!=0)
					string1 += ",";																							//if current textField is not the first textfield then put a trailing ",".
				tempCount++;																								//incrementing value of tempCount
				id++;																										//incrementing value of id
				string1 += "{'id':'"+id+"','componentName':'"+ie.textField(i).name()+ "','componentType':'TextField',}";				//adding textfield to string1 in the form of {'id':'<id>','cmpname':'<cmpname>','componentType':'TextField'}
			}
				if(i!=0)
					flag = true;																							//changing value of flag to true if there is any textfield on the page i.e a trailing "," is missing
				finalCount += tempCount;																					//changing value of finalCount
				tempCount = 0;																								//changing value of tempCount to 0
			
			for(i=0;i<radiobutton.length();i++)
			{
				if(i>0 && !(ie.radio(i).name().equals(ie.radio(i-1).name())))												//if name of the radio button is != to name of previous button then only add it to string, otherwise not. As name of all the radio buttons of same group is same.
				{
					tempCount++;																							//incrementing value of tempCount
					id++;																									//incrementing value of id
					string1 += ",{'id':'"+id+"','componentName':'"+ie.radio(i).name()+ "','componentType':'Radio'}";					//adding radiobutton to string1 in the form of {'id':'<id>','componentName':'<componentName>','componentType':'Radio'}
				}
				else if(i==0 && flag == true)
				{
					tempCount++;																							//incrementing value of tempCount
					id++;																									//incrementing value of id
					string1 += ",{'id':'"+id+"','componentName':'"+ie.radio(i).name()+ "','componentType':'Radio'}";					//adding radiobutton to string1 in the form of {'id':'<id>','componentName':'<componentName>','componentType':'Radio'}
				}
				else if(i==0 && flag == false)
				{
					tempCount++;																							//incrementing value of tempCount
					id++;																									//incrementing value of id
					string1 += "{'id':'"+id+"','componentName':'"+ie.radio(i).name()+ "','componentType':'Radio'}";						//adding radiobutton to string1 in the form of {'id':'<id>','componentName':'<componentName>','componentType':'Radio'}
				}				
			}
			
			finalCount += tempCount;																						//changing value of finalCount
			tempCount = 0;																									//changing value of tempCount to 0
				if(i!=0)
					flag = true;																							//changing value of flag to true if there is any radiobutton on the page i.e a trailing "," is missing
				
			for(i=0;i<checkbox.length();i++)
			{
				if(i>0 && !(ie.checkbox(i).name().equals(ie.checkbox(i-1).name())))											//if name of the checkbox is != to name of previous checkbox then only add it to string, otherwise not. As name of all the checkbox of same group is same.
				{
					tempCount++;																							//incrementing value of tempCount
					id++;																									//incrementing value of id
					string1 += ",{'id':'"+id+"','componentName':'"+ie.checkbox(i).name()+ "','componentType':'CheckBox'}";				//adding textfield to string1 in the form of {'id':'<id>','componentName':'<componentName>','componentType':'CheckBox'}
				}
				else if(i==0 && flag)
				{
					tempCount++;																							//incrementing value of tempCount
					id++;																									//incrementing value of id
					string1 += ",{'id':'"+id+"','componentName':'"+ie.checkbox(i).name()+ "','componentType':'CheckBox'}";				//adding textfield to string1 in the form of {'id':'<id>','componentName':'<componentName>','componentType':'CheckBox'}
				}
				else if(i==0 && !flag)
				{
					tempCount++;																							//incrementing value of tempCount
					id++;																									//incrementing value of id
					string1 += "{'id':'"+id+"','componentName':'"+ie.checkbox(i).name()+ "','componentType':'CheckBox'}";				//adding textfield to string1 in the form of {'id':'<id>','componentName':'<componentName>','componentType':'CheckBox'}
				}
			}
			
			finalCount += tempCount;																						//changing value of finalCount
			tempCount = 0;																									//changing value of tempCount to 0
			if(i!=0)
				flag = true;																								//changing value of flag to true if there is any checkbox on the page i.e a trailing "," is missing
			
			for(i=0;i<list.length();i++)
			{
				if(flag)
				{
					tempCount++;																							//incrementing value of tempCount
					id++;																									//incrementing value of id
					string1 += ",{'id':'"+id+"','componentName':'"+ie.selectList(i).name()+ "','componentType':'SelectList'}";			//adding combobox to string1 in the form of {'id':'<id>','componentName':'<componentName>','componentType':'SelectList'}
				}
				else if(!flag)
				{
					tempCount++;																							//incrementing value of tempCount
					id++;																									//incrementing value of id
					string1 += "{'id':'"+id+"','componentName':'"+ie.selectList(i).name()+ "','componentType':'SelectList'}";			//adding combobox to string1 in the form of {'id':'<id>','componentName':'<componentName>','componentType':'SelectList'}
				}
			}
			
			finalCount += tempCount;																						//changing value of finalCount
			tempCount = 0;																									//changing value of tempCount to 0
			
			if(i!=0)
				flag = true;																								//changing value of flag to true if there is any selectList on the page i.e a trailing "," is missing

			String[] hidden1 = new String[hidden.length()];
			int mark = 0;
			for(i=0;i<hidden.length();i++)
			{
				if(flag)
				{																								//incrementing value of id
					if(mark == 0)
					{																							//incrementing value of tempCount
						id++;	
						string1 += ",{'id':'"+id+"','componentName':'"+ie.hidden(i).name()+ "','componentType':'Hidden'}";						//adding hidden element to string1 in the form of {'id':'<id>','componentName':'<componentName>','componentType':'Hidden'}
						tempCount++;
						hidden1[mark] = ie.hidden(i).name();
						System.out.println(hidden1[mark]);
						mark++;
					}
					else if(mark != 0)
					{
						boolean occur = false;
						for(int j=0;j<mark;j++)
							{
							if(!(ie.hidden(i).name().equals(hidden1[j])))
								occur = false;
							else if(ie.hidden(i).name().equals(hidden1[j]))
								{
								occur = true;
								}
							if(occur)
								break;
							}
						if(occur == false)
						{																							//incrementing value of tempCount
							id++;	
							string1 += ",{'id':'"+id+"','componentName':'"+ie.hidden(i).name()+ "','componentType':'Hidden'}";						//adding hidden element to string1 in the form of {'id':'<id>','componentName':'<componentName>','componentType':'Hidden'}
							tempCount++;
							hidden1[mark] = ie.hidden(i).name();
							System.out.println(hidden1[mark]);
							mark++;
							}
					}
				}
				else if(!flag)
				{
					if(mark == 0)
					{
						tempCount++;
						id++;																									//incrementing value of id
						string1 += "{'id':'"+id+"','componentName':'"+ie.hidden(i).name()+ "','cmptype':'Hidden'}";					//adding hidden element to string1 in the form of {'id':'<id>','componentName':'<componentName>','cmptype':'Hidden'}
						hidden1[mark] = ie.hidden(i).name();
						System.out.println(hidden1[mark]);
						mark++;
					}
					else if(mark != 0)
					{
						boolean occur = false;
						for(int j=0;j<mark;j++)
						{
							if(!(ie.hidden(i).name().equals(hidden1[j])))
								occur = false;
							else if(ie.hidden(i).name().equals(hidden1[j]))
								{
								occur = true;
								}
							if(occur)
								break;
						}
						if(occur == false)
						{																							//incrementing value of tempCount
							id++;	
							string1 += ",{'id':'"+id+"','componentName':'"+ie.hidden(i).name()+ "','componentType':'Hidden'}";						//adding hidden element to string1 in the form of {'id':'<id>','componentName':'<componentName>','componentType':'Hidden'}
							tempCount++;
							hidden1[mark] = ie.hidden(i).name();
							System.out.println(hidden1[mark]);
							mark++;
							}
					}
				}
			}
			
			finalCount += tempCount;																						//incrementing value of finalCount
			jsonResponse = "{'success':'true','size':'"+finalCount+"','data':"+string1+"]}";								//Complete jsonResponse string
			pw.println(jsonResponse);
			ie.close();
		}
		catch(Exception ex)
		{
			pw.print(ex.getMessage());
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
/*
 * Written By : Anshu Kumar (email:ansharyan015@gmail.com)
*/